/*
 * Copyright (C) 2014 Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.bambole.facade;

import com.googlecode.bambole.controller.util.ConfigurationUtil;
import com.googlecode.bambole.domain.Address;
import com.googlecode.bambole.domain.Channel;
import com.googlecode.bambole.domain.OrderHeader;
import com.googlecode.bambole.domain.Organization;
import com.googlecode.bambole.domain.PaymentTerm;
import com.googlecode.bambole.domain.Person;
import com.googlecode.bambole.domain.Status;
import com.googlecode.bambole.domain.Type;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 */
@Stateless
public class OrderHeaderFacade extends AbstractFacade<OrderHeader> {

    @PersistenceContext(unitName = "BambolePU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public OrderHeaderFacade() {
        super(OrderHeader.class);
    }

    public int count(String query, Object[] searchArray) {
        javax.persistence.Query q = getEntityManager().createQuery(query);

        if (searchArray[0] != null) {
            q.setParameter("type", (Type) searchArray[0]);
        }

        if (searchArray[1] != null) {
            q.setParameter("status", (Status) searchArray[1]);
        }

        if (searchArray[2] != null) {
            q.setParameter("channel", (Channel) searchArray[2]);
        }

        if (searchArray[3] != null) {
            q.setParameter("paymentTerm", (PaymentTerm) searchArray[3]);
        }

        if (searchArray[4] != null) {
            q.setParameter("organization", (Organization) searchArray[4]);
        }

        if (searchArray[5] != null) {
            q.setParameter("address", (Address) searchArray[5]);
        }

        if (searchArray[6] != null) {
            q.setParameter("person", (Person) searchArray[6]);
        }

        if (searchArray[7] != null) {
            q.setParameter("freightOrganization", (Organization) searchArray[7]);
        }

        if (searchArray[8] != null) {
            q.setParameter("freightOrganizationPerson", (Person) searchArray[8]);
        }

        return q.getResultList().size();
    }

    public List<OrderHeader> findRange(int[] range, String query, Object[] searchArray) {
        javax.persistence.Query q = getEntityManager().createQuery(query);

        if (searchArray[0] != null) {
            q.setParameter("type", (Type) searchArray[0]);
        }

        if (searchArray[1] != null) {
            q.setParameter("status", (Status) searchArray[1]);
        }

        if (searchArray[2] != null) {
            q.setParameter("channel", (Channel) searchArray[2]);
        }

        if (searchArray[3] != null) {
            q.setParameter("paymentTerm", (PaymentTerm) searchArray[3]);
        }

        if (searchArray[4] != null) {
            q.setParameter("organization", (Organization) searchArray[4]);
        }

        if (searchArray[5] != null) {
            q.setParameter("address", (Address) searchArray[5]);
        }

        if (searchArray[6] != null) {
            q.setParameter("person", (Person) searchArray[6]);
        }

        if (searchArray[7] != null) {
            q.setParameter("freightOrganization", (Organization) searchArray[7]);
        }

        if (searchArray[8] != null) {
            q.setParameter("freightOrganizationPerson", (Person) searchArray[8]);
        }

        q.setMaxResults(range[1] - range[0] + 1);
        q.setFirstResult(range[0]);

        return q.getResultList();
    }

    public int findMaxDocument() {
        Query query = em.createNamedQuery("OrderHeader.findMaxDocument");
        return query.getSingleResult() == null ? 1 : (int) query.getSingleResult() + 1;
    }

    public int getNextDocument() {
        int documentNumber = ConfigurationUtil.getOrderDocumentNumber();
        if (documentNumber == -1) {
            Query query = em.createNamedQuery("OrderHeader.findMaxDocument");
            return query.getSingleResult() == null ? 1 : (int) query.getSingleResult() + 1;
        } else {
            try {
                Query query = em.createNamedQuery("OrderHeader.findByDocument");
                query.setParameter("document", documentNumber);
                while (query.getSingleResult() != null) {
                    documentNumber = ConfigurationUtil.getOrderDocumentNumber();
                    query.setParameter("document", documentNumber);
                }
            } catch (NoResultException nre) {
                Logger.getAnonymousLogger().log(Level.INFO, "OrderHeader not found: {0}", nre.getMessage());
            }
        }
        return documentNumber;
    }
}
